#include "stdafx.h"
#include "DBManager.h"
#include <xml.h>
#include <common.h>
#include <boost/bind.hpp>
#include "VerifyTask.h"

using namespace utility::database;
using namespace utility::system;
using namespace utility::xml;

#pragma comment(lib, "database_module.lib")
#pragma comment(lib, "system.lib")

CDBManager::CDBManager(void)
{
    m_pDBModule = get_database_module();
    database_connection * pConnection = m_pDBModule->create_connection(MYSQL_DB);
}


CDBManager::~CDBManager(void)
{
    destroy_database_module();
}

bool CDBManager::Init()
{
    std::string strConfigFile = get_exe_path();
    strConfigFile += "/DBConfig.xml";
    return _Load(strConfigFile.c_str());
}

bool CDBManager::_Load( const char * szFileName )
{
    utility::xml::CMarkupSTL XmlObj;

    if (!XmlObj.Load(szFileName))
    {
        return false;
    }

    if (!XmlObj.FindElem("configure"))
    {
        return false;
    }

    XmlObj.IntoElem();
    if (XmlObj.FindElem("connection"))
    {
        std::string strType = XmlObj.GetAttrib("type");
        if (strType.empty())
        {
            return false;
        }
        std::string strHost = XmlObj.GetAttrib("host");
        if (strHost.empty())
        {
            return false;
        }
        std::string strPort = XmlObj.GetAttrib("port");
        if (strPort.empty())
        {
            return false;
        }
        std::string strUser = XmlObj.GetAttrib("user");
        if (strUser.empty())
        {
            //LOG(CRITICAL) << "failed to find XML attribution: user.";
            return false;
        }
        std::string strPwd = XmlObj.GetAttrib("pwd");
        if (strPwd.empty())
        {
            //LOG(CRITICAL) << "failed to find XML attribution: pwd.";
            return false;
        }
        std::string strDB = XmlObj.GetAttrib("db");
        if (strDB.empty())
        {
            //LOG(CRITICAL) << "failed to find XML attribution: db.";
            return false;
        }
        if (strType == "mysql")
            m_pConnection = m_pDBModule->create_connection(MYSQL_DB);
        else 
            return false;

        if (!m_pConnection->connect(strHost.c_str(), strUser.c_str(), strPwd.c_str(), strDB.c_str(), atoi(strPort.c_str())))
            return false;

        return true;
    }

    return false;
}

bool CDBManager::Run()
{
    return m_pDBModule->run();
}

void CDBManager::VerifyAccount( const char * szUser, const char * szPassword, TCallback fCallback )
{
     SQLcommand_pointer command = m_pConnection->create_command("SELECT UserId FROM tblUser WHERE UserName=@name AND Password=@pwd");
     command->parameters.add<PARAM::STRING>("@name")->value = szUser;
     command->parameters.add<PARAM::STRING>("@pwd")->value = szPassword;
     CVerifyTask::pointer pVerifyTask = CVerifyTask::pointer(new CVerifyTask(szUser, szPassword, fCallback));
     command->async_query(boost::bind(&CVerifyTask::DatabaseCallback, pVerifyTask, _1, _2));
}
